--winuser submodule that deals with dlls.
setfenv(1, require'winapi')

ffi.cdef[[
struct HINSTANCE__ { int unused; }; typedef struct HINSTANCE__ *HINSTANCE;
typedef HINSTANCE HMODULE;

HMODULE GetModuleHandleW(
     LPCWSTR lpModuleName
    );

BOOL GetModuleHandleExW(
			DWORD dwFlags,
         LPCWSTR lpModuleName,
         HMODULE* phModule
    );

HMODULE LoadLibraryW(LPCWSTR lpLibFileName);
]]

function GetModuleHandle(name)
	return checkh(ffi.C.GetModuleHandleW(wcs(name)))
end

GET_MODULE_HANDLE_EX_FLAG_PIN                 = 0x00000001
GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT  = 0x00000002
GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS        = 0x00000004

function GetModuleHandleEx(name, GMHX)
	local h = ffi.new('HMODULE')
	checkz(ffi.C.GetModuleHandleExW(flags(GMHX), wcs(name), h))
	return h
end

function LoadLibrary(filename)
    return checkh(ffi.C.LoadLibraryW(wcs(filename)))
end

if not ... then
print(GetModuleHandle())
print(LoadLibrary'shell32')
end
